﻿/*
 *  XTL - XNA Tool Library, a collection with usefull XNA classes
 * 
 *  Copyright (C) 2009 Sebastian Schäfer
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XTL.Display
{
    /// <summary>
    /// A class to show some text onscreen
    /// </summary>
    public class HUDText : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private SpriteBatch spriteBatch;
        private SpriteFont font;
        private String fontname;

        /// <summary>
        /// Color of text
        /// </summary>
        public Color Color { get; set; }
        /// <summary>
        /// The position where the text will apppear
        /// </summary>
        public Vector2 Position { get; set; }
        /// <summary>
        /// The text that will be shown
        /// </summary>
        public String Text{get;set;}

        /// <summary>
        /// The font used to show some Text. If not set Kootenay is used
        /// </summary>
        public SpriteFont Font
        {
            get
            {
                return font;
            }
            set
            {
                font = value;
            }
        }
        
        /// <summary>
        /// The name of the font-asset that is used. If set it will be loaded.
        /// </summary>
        public String Fontname
        {
            get
            {
                return fontname;
            }
            set
            {
                fontname = value;
                if(font != null)
                    LoadFont();
            }
        }

        /// <summary>
        /// Create a new instance
        /// </summary>
        /// <param name="game">[...]</param>
        /// <param name="fontname">Assetname of the font</param>
        public HUDText(Game game, String fontname)
            : base(game)
        {
            this.Color = Color.White;
            this.Position = new Vector2(10, 10);
            this.Text = "";
            this.fontname = fontname;
        }

        /// <summary>
        /// Create a new instance, set assetname to to "font"
        /// </summary>
        /// <param name="game">[...]</param>
        public HUDText(Game game)
            : this(game, "font")
        {
        }

        /// <summary>
        /// Call to base Initialize
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// LoadContent to load the font Kootenay
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            LoadFont();
        }

        /// <summary>
        /// Load font by assigned assetname
        /// </summary>
        public void LoadFont()
        {
            try
            {
                font = Game.Content.Load<SpriteFont>(fontname);
            }
            catch
            {
                throw new Exception("Couldn't load font");
            }
        }
        
        /// <summary>
        /// [...]
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Nothing to be done in update
        /// </summary>
        /// <param name="gameTime">[...]</param>
        public override void Update(GameTime gameTime)
        {
        }

        /// <summary>
        /// Draw the Text
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            PrintText(Text);
        }

        /// <summary>
        /// Draw the text on the screen
        /// </summary>
        /// <param name="txt">The text that is drawn</param>
        protected void PrintText(String txt)
        {
            if (Enabled)
            {
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
                spriteBatch.DrawString(font, txt, this.Position, this.Color);
                spriteBatch.End();
            }
        }
    }
}
